package sealedGenerator.card;

import java.awt.Image;
import java.awt.Toolkit;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import javax.swing.ImageIcon;

import sealedGenerator.Set;

public class Card {
	private final ManaCost manaCost;
	private final Integer convertedManaCost;
	private final ArrayList<Color> color;
	private final String name;
	private final String superType;
	private final String type;
	private final String subType;
	private final String rulesText;
	private final Integer power;
	private final Integer toughness;
	private final Rarity rarity;
	private final Set set;
	private final Integer collectorNumber;
	
	//encapsulates all information about a M:TG card
	public Card(String manaCost, Integer cmc, ArrayList<Color> color, String name,
			String superType, String type, String subType, String rulesText,
			Integer power, Integer toughness, Rarity rarity, Set set, Integer collectorNumber){
		this.collectorNumber = collectorNumber;
		this.manaCost=new ManaCost(manaCost);
		this.convertedManaCost=cmc;
		this.color=color;
		this.name=name;
		this.superType=superType;
		this.type=type;
		this.subType=subType;
		this.rulesText=rulesText;
		this.power=power;
		this.toughness=toughness;
		this.rarity=rarity;
		this.set=set;
	}
	public ManaCost getManaCost(){return manaCost;}
	public Integer getCMC(){return convertedManaCost;}
	public ArrayList<Color> getColors() {return color;}
	public String getName() {return name;}
	public String getSuperType() {return superType;}
	public String getType() {return type;}
	public String getSubType() {return subType;}
	public String getRulesText() {return rulesText;}
	public Integer getPower() {return power;}
	public Integer getToughness() {return toughness;}
	public Rarity getRarity() {return rarity;}
	public Set getSet() {return set;}
	public Integer getCollectorNumber() {return collectorNumber;}
	
	public boolean isColor(Color wantedColor) {
		return color.contains(wantedColor);
	}
	@Override
	public String toString(){
		StringBuilder returnString=new StringBuilder();
		returnString.append(name);
		returnString.append(" ");
		
		if(manaCost==null){
			returnString.append("(none)");
		} else{
			returnString.append("(");
			returnString.append(manaCost);
			
			if(color != null && color.size() > 0) {
				returnString.append(" - ");
				
				for(Color c: color) {
					if(c != color.get(0))
						returnString.append(", ");
					
					returnString.append(c);
				}
			}
			
			returnString.append(")");
		}
		
		returnString.append(" ");
		if(superType !=null){
			returnString.append(superType);
			returnString.append(" ");
		}
		returnString.append(type);
		if(subType != null){
			returnString.append("-");
			returnString.append(subType);
		}
		returnString.append(" ");
		if(rulesText !=null){
			returnString.append(rulesText);
			returnString.append(" ");
		}
		if(power !=null){
			returnString.append(power);
			returnString.append("/");
			returnString.append(toughness);
			returnString.append(" ");
		}
		returnString.append(rarity);
		// returnString.append(" ");
		// returnString.append(set);
		return returnString.toString();
	}
	
	public String getMWSDeckString(int count) {
		return "    " + count + " [" + getSet().getExpansionCode() + "] " + getName() + "\r\n";
	}
	
	public boolean equals(Card c){
		if (!c.getName().equals(name))
			return false;
		if (!c.getSet().getName().equals(set.getName()))
			return false;
		return true;
	}
	
	public URL getImageURL(){
		try{
			URL url = new URL("http://magiccards.info/scans/en/"+set.getExpansionCode().toLowerCase()+"/"+collectorNumber+".jpg");
			return url;
		} catch(MalformedURLException mue){
			mue.printStackTrace();
		}
		return null;
	}
	
	/**sortCode is a string containing any number of the following codes:
	 * C = color
	 * M = mana cost
	 * Y = card type
	 * E = edition
	 * P = power
	 * T = toughness
	 * R = rarity
	 * 
	 * Note: all sorts will be filtered at the last level by Name (and then edition if they have the same name)
	 * For a name only sort, pass an empty string
	 * 
	 * Example:
	 * CM - will sort by color, then mana cost, then name.
	 */
	public static List<Card> sort(String sortCode, List<Card> cards){
		CardComparator com = new CardComparator(sortCode);
		Collections.sort(cards, com);
		return cards;
	}
}